Pointers in CPP
A pointer is a variable that holds the address of another variable in memory. Pointers are essential for managing dynamic memory, manipulating arrays, effectively passing big structures to functions, and enabling intricate data structures such as linked lists, trees, and graphs.
Pointer Basics:
Rather than the value of a variable, a pointer contains the variable's address. A pointer is a container for an address that points to the location of the data in memory, if you think of variables in C++ as data containers.
Declaring a Pointer:
An asterisk (*) is placed before the pointer variable name when declaring a pointer, along with the data type to which the pointer points.
Syntax:
datatype *var_name;
Example:
- int* indicates that ptr is a pointer to an int.
- ptr can store the memory address of an integer variable.
How Pointers Work:
A pointer is not always instantly pointed to a specific address in memory when it is declared. It has to be assigned a variable's address using the address-of operator (&).
Example:
- num is an integer variable with a value of 10.
- &num is the address of the variable num.
- ptr is a pointer variable that holds the address of num
Dereferencing a Pointer:
The dereference operator (*) is used to retrieve the value that is kept at the memory address that a pointer is referring to.
Example:
*ptrdereferences the pointer, accessing the value stored at the address held byptr.
Arithmetic Pointer:
With arrays in particular, pointers can be increased or decremented to point to nearby memory regions. arithmetic pointer in C++ is dependent on the size of the data type that is being referenced.
Example:
- When you increment the pointer (
ptr++), it advances by the size of the type it points to (in this case, bysizeof(int)).
Pointers and Arrays:
In reality, an array name is only a constant pointer to the array's first entry. As a result, pointer arithmetic can be used to alter array elements.
Null Pointer:
A pointer that points to no valid memory location is called a null pointer. Initializing pointers with NULL (earlier C++) or nullptr .
Pointer to Pointer (Double Pointer):
A pointer that holds the address of another pointer is called a pointer to pointer. It comes in handy when you need to manage pointers at different levels or send pointers to functions.
- pptr is a pointer that holds the address of ptr.
- **pptr dereferences pptr twice to get the value of num.
Dynamic Memory Allocation:
- Allocating memory at run-time.
- Allocate and deallocate memory using “new” and “delete” operator
- new - allocates memory to a variable.
- delete - no longer needed
- It returns memory to the operating system
- This is known as deallocation.
Example:
Pointers and Functions:
Large objects (such arrays or structures) can be effectively passed to functions by using pointers rather than copying the entire item.
Pass by Pointer Example:
- the function
increment()modifies the original variable because it is passed the memory address ofnum.
Pointers to Functions:
The address of a function is stored in a pointer to a function. Callback functions and polymorphism implementation benefit from this.
Example:
Void Pointers:
A general pointer that can store the address of any kind of data is a void pointer. Before dereferencing it, the proper type must be cast.
Example:
Comments